﻿using PhotoKiosk.Model;
using PhotoKiosk.Properties;
using PhotoKiosk.ViewModel.Helpers;
using System.Collections.Generic;
using System.Globalization;
using System.Resources;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel
{
    public class PhotosSelectViewModel : ViewModelBase
    {
        private const int k_PhotosInPage = 10;
        private MainWindowViewModel m_mainWindowViewModel;

        private List<PhotoSectionViewModel> m_allPhotos;
        public List<PhotoSectionViewModel> m_viewPhotos;
        public List<PhotoSectionViewModel> ViewPhotos
        {
            get
            {
                if (m_viewPhotos == null)
                {
                    m_viewPhotos = new List<PhotoSectionViewModel>();
                }
                return m_viewPhotos;
            }
            set
            {
                m_viewPhotos = value;
                OnPropertyChanged("ViewPhotos");
            }
        }
              
        private int m_currentPage = 0;
        public int CurrentPage
        {
            get
            {
                return m_currentPage;
            }
            set
            {
                m_currentPage = value;
                OnPropertyChanged("CurrentPageView");
            }
        }

        public int CurrentPageView
        {            
            get
            {
                return m_currentPage + 1;
            }
        }

        private int m_maxPage = 1;
        public int MaxPage
        {
            get
            {
                return m_maxPage;
            }
            set
            {
                m_maxPage = value;
                OnPropertyChanged("MaxPageView");
            }
        }

        public int MaxPageView
        {
            get
            {
                return m_maxPage + 1;
            }
        }
        
        public string Price
        {
            get
            {
                return RegionInfo.CurrentRegion.CurrencySymbol + " " + m_mainWindowViewModel.SystemData.OrderPrice().ToString();
            }
        }

        public PhotosSelectViewModel(MainWindowViewModel i_mainWindowViewModel)
        {
            this.m_mainWindowViewModel = i_mainWindowViewModel;
            loadPhotos();
        }

        private void loadPhotos()
        {
            m_allPhotos = new List<PhotoSectionViewModel>();
            foreach (Photo p in m_mainWindowViewModel.SystemData.ConnectedDevice.AllPhotos())
            {
                PhotoSectionViewModel psvm = new PhotoSectionViewModel(p);
                psvm._ClickHandler += changePriceEvent;
                m_allPhotos.Add(psvm);
            }
            MaxPage = (m_allPhotos.Count / k_PhotosInPage);
            updatePage();
        }

        private void changePriceEvent()
        {
            OnPropertyChanged("Price");
        }

        #region Finish command
        private DelegateCommand m_finishCommand;
        public ICommand FinishCommand
        {
            get
            {
                if (m_finishCommand == null)
                {
                    m_finishCommand = new DelegateCommand(param => this.finishCommandExecute(), param => this.finishCommandCanExecute());
                }
                return m_finishCommand;
            }

        }

        private bool finishCommandCanExecute()
        {
            return isMoreThenOnePhotoSelected();
        }

        private void finishCommandExecute()
        {
            m_mainWindowViewModel.CurrentViewModel = new ProcessingViewModel(m_mainWindowViewModel, () => FinishOrder(),m_mainWindowViewModel.ResourceManager.GetString("While_Copying_Your_Images_To_The_Computer"));
        }

        public void FinishOrder()
        {
            m_mainWindowViewModel.SystemData.FinishOrder();
            m_mainWindowViewModel.CurrentViewModel = new FinishOrderViewModel(m_mainWindowViewModel);
        }

        private bool isMoreThenOnePhotoSelected()
        {
            foreach (PhotoSectionViewModel psvm in m_allPhotos)
            {
                if (psvm.IsSelected)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region Pages navigation commands
        private DelegateCommand m_previousPageCommand;
        public ICommand PreviousPageCommand
        {
            get
            {
                if (m_previousPageCommand == null)
                {
                    m_previousPageCommand = new DelegateCommand(param => this.previousPageCommandExecute(), param => this.previousPageCommandCanExecute());
                }
                return m_previousPageCommand;
            }
        }

        private bool previousPageCommandCanExecute()
        {
            return (CurrentPage != 0);
        }

        private void previousPageCommandExecute()
        {
            CurrentPage--;
            updatePage();
        }

        private DelegateCommand m_nextPageCommand;
        public ICommand NextPageCommand
        {
            get
            {
                if (m_nextPageCommand == null)
                {
                    m_nextPageCommand = new DelegateCommand(param => this.nextPageCommandExecute(), param => this.nextPageCommandCanExecute());
                }
                return m_nextPageCommand;
            }

        }

        private bool nextPageCommandCanExecute()
        {
            return (CurrentPage != MaxPage);
        }

        private void nextPageCommandExecute()
        {
            CurrentPage++;
            updatePage();
        }

        private void updatePage()
        {
            List<PhotoSectionViewModel> d = new List<PhotoSectionViewModel>();
            if (CurrentPage >= 0 && CurrentPage <= MaxPage)
            {
                for (int i=0 ; i<k_PhotosInPage; i++)
                {
                    if (k_PhotosInPage * (CurrentPage) + i < m_allPhotos.Count)
                    {
                        d.Add(m_allPhotos[k_PhotosInPage * (CurrentPage) + i]);
                    }
                }
            }
            ViewPhotos = d;
        }
        #endregion
    }
}
